//
//  SuggestMessageViewController.m
//  JiuKuGospel
//
//  Created by jiukuyinyue on 15-12-21.
//  Copyright (c) 2015年 JiuKuForApp. All rights reserved.
//

#import "SuggestMessageViewController.h"

@interface SuggestMessageViewController ()<UITextViewDelegate, UITextFieldDelegate>
@property (nonatomic, strong)UIScrollView * scrollView;
@property (nonatomic, strong)UITextView * textView;
@property (nonatomic, strong)UILabel * placeholderLabel;
@property (nonatomic, strong)UITextField * textField;
@property (nonatomic, strong)UIImageView * selectImageView;
@property (nonatomic, strong)NSString * typeStr;
@end

@implementation SuggestMessageViewController

- (BOOL)canBecomeFirstResponder{
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"意见反馈";
    self.view.backgroundColor = RGBColor(227, 227,227);

    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightBtn setTitle:@"提交" forState:UIControlStateNormal];
    [rightBtn setTitleColor:RGBColor(89, 196, 120) forState:UIControlStateNormal];
    rightBtn.titleLabel.font = FONT_16;
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    
    [self setupUI];
}
- (void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightBtnAction
{
    if (_textView.text.length == 0) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:nil message:@"请填写反馈内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        //反馈意见请求
        NSDictionary * dic = @{@"type":@"suggest", @"tag":@"iOS", @"content":[NSString stringWithFormat:@"%@:%@",self.typeStr, _textView.text], @"contact":_textField.text};
        
        [LSZRequestData initregistrationSendVerificationCode:dic url:kBasePostFuyin :^(BOOL isOK, NSString * str, NSDictionary * diction) {
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
            hud.mode=MBProgressHUDModeText;//枚举类型不同的效果
            
            NSString * status = nil;
            if ([[diction objectForKey:@"status"] isKindOfClass:[NSNumber class]]) {
                status = [[diction objectForKey:@"status"] stringValue];
            }
            if ([[diction objectForKey:@"status"] isKindOfClass:[NSString class]]) {
                status = [diction objectForKey:@"status"];
            }

            if ([status isEqualToString:@"200"]) {
                hud.label.text=[NSString stringWithFormat:@"%@反馈提交成功", @""];
                hud.label.backgroundColor= [UIColor whiteColor];
                [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
            }
            if ([status isEqualToString:@"400"]) {
                hud.label.text=[NSString stringWithFormat:@"%@反馈提交失败", @""];
                hud.label.backgroundColor= [UIColor whiteColor];
                [self performSelector:@selector(hiddenMBAction) withObject:nil afterDelay:1.0];
            }
        }];
    }
}
- (void)setupUI
{
    self.selectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 24, 24)];
    _selectImageView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_yes"];
    _selectImageView.userInteractionEnabled = YES;
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap)];
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT, ScreenWidth, ScreenHeight)];
    [_scrollView addGestureRecognizer:tap];
    [self.view addSubview:_scrollView];
    
    UILabel * typesLabel = [[UILabel alloc] initWithFrame:CGRectMake(10,10, self.scrollView.frame.size.width-20, 25)];
    typesLabel.text = @"反馈类型";
    typesLabel.font = FONT_16;
    [_scrollView addSubview:typesLabel];
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0,CGRectGetMaxY(typesLabel.frame)+10, typesLabel.frame.size.width, 0.5)];
    line.backgroundColor = [UIColor lightGrayColor];
    [_scrollView addSubview:line];
    //反馈类型  select_ok@3x.png
    UITapGestureRecognizer * selectTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * chanpinView = [[UIImageView alloc] initWithFrame:CGRectMake(10, typesLabel.bottom+20, 24, 24)];
    chanpinView.userInteractionEnabled = YES;
    chanpinView.tag = 1501;
    chanpinView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [chanpinView addGestureRecognizer:selectTap];
    [_scrollView addSubview:chanpinView];
    UILabel * chanpinLabel = [[UILabel alloc] initWithFrame:CGRectMake(chanpinView.right+5, chanpinView.top, self.view.frame.size.width/3-chanpinView.right-5, chanpinView.height)];
    chanpinLabel.text = @"产品建议";
    chanpinLabel.font = FONT_15;
    [_scrollView addSubview:chanpinLabel];

    UITapGestureRecognizer * selectTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * chengxuView = [[UIImageView alloc] initWithFrame:CGRectMake(chanpinLabel.right+10, chanpinLabel.top, 24, 24)];
    chengxuView.userInteractionEnabled = YES;
    chengxuView.tag = 1502;
    chengxuView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [chengxuView addGestureRecognizer:selectTap1];
    [_scrollView addSubview:chengxuView];
    UILabel * chengxuLabel = [[UILabel alloc] initWithFrame:CGRectMake(chengxuView.right+5, chengxuView.top, self.view.width/3*2-chengxuView.right-5, chengxuView.height)];
    chengxuLabel.text = @"程序错误";
    chengxuLabel.font = FONT_15;
    [_scrollView addSubview:chengxuLabel];

    UITapGestureRecognizer * selectTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectType:)];
    UIImageView * contentView = [[UIImageView alloc] initWithFrame:CGRectMake(chengxuLabel.right+10, chengxuLabel.top, 24, 24)];
    contentView.userInteractionEnabled = YES;
    contentView.tag = 1503;
    contentView.image = [UIImage imageNamed:@"我的_设置_关于_意见反馈_no"];
    [contentView addGestureRecognizer:selectTap2];
    [_scrollView addSubview:contentView];
    UILabel * contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(contentView.right+5, contentView.top, self.view.width-contentView.right-5, contentView.height)];
    contentLabel.text = @"缺少内容";
    contentLabel.font = FONT_15;
    [_scrollView addSubview:contentLabel];

    [chanpinView addSubview:_selectImageView];
    self.typeStr = @"产品建议";
    //意见
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(10, chanpinView.bottom+10, self.view.width-2*10, 80)];
    _textView.font = FONT_16;
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_textView];
    self.placeholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, _textView.width-10, 30)];
    _placeholderLabel.text = @"您的反馈或建议会使我们的软件更加完美";
    _placeholderLabel.font = FONT_15;
    _placeholderLabel.textColor = [UIColor lightGrayColor];
    [_textView addSubview:_placeholderLabel];
    
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(_textView.left, _textView.bottom+15, _textView.width, 40)];
    _textField.delegate = self;
    _textField.font = FONT_15;
    _textField.placeholder = @"请填写您的联系方式(QQ或手机号)";
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.keyboardType = UIKeyboardTypeNumberPad;
    [_scrollView addSubview:_textField];
}
- (void)tap
{
    _scrollView.contentSize = CGSizeMake(self.view.width, _scrollView.height);
    [self.view endEditing:YES];
}
- (void)selectType:(UITapGestureRecognizer *)tap
{
    UIImageView * tempView = (UIImageView *)tap.view;
    [tempView addSubview:_selectImageView];
    if (tempView.tag == 1501) {
        self.typeStr = @"产品建议";
    }
    if (tempView.tag == 1502) {
        self.typeStr = @"程序错误";
    }
    if (tempView.tag == 1503) {
        self.typeStr = @"缺少内容";
    }
}
-(void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length == 0) {
        _placeholderLabel.hidden = NO;
    }else{
        _placeholderLabel.hidden = YES;
    }
}
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    if (self.view.height == 480) {
        _scrollView.contentSize = CGSizeMake(self.view.width, self.view.height+70);
    }
    return YES;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    if (self.view.height == 480) {
        _scrollView.contentSize = CGSizeMake(self.view.width, self.view.height+70);
    }
    return YES;
}
-(void)hiddenMBAction
{
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    self.textView.text = @"";
    self.textField.text = @"";
    self.placeholderLabel.hidden = NO;
}
@end
